﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SqlDao;
using System.Configuration;
using System.Diagnostics;
using System.Reflection;
using System.Threading;



namespace SqlDaoDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            //TestSelect();

          
        }

        private void TestInsert()
        {
          
        }
        private void TestDelete()
        {

        }
        private void TestSelect()
        {
            String connstr = ConfigurationManager.ConnectionStrings["mysqlConn"].ConnectionString.ToString();
            MySqlHelper mySqlHelper = new MySqlHelper(connstr);
            DbHelper.enableErrorLog  =true;
            DbHelper.isDebug =true;

            for (int i = 0; i < 10; i++)
            {
                new Thread(() => {
                    string sql = "SELECT * FROM car_info ;";
                    List<CarInfo> cars = mySqlHelper.Select<CarInfo>(sql);
                    foreach (var item in cars)
                    {
                        Debug.WriteLine("id:" + item.Id + " number:" + item.carNumber + " driver:" + item.driver + " phone:" + item.driverMobile + " status:" + item.deleteTime);
                    }
                    var car = new CarInfo()
                    {
                        id = 1,
                        ownerName = "车主",
                        icNumber = String.Empty
                    };
                    //修改时，定义的字段有默认的字段，没有赋值时，会自动采用默认值，导致数据修改出错
                    //设置 Nullable 类型可以解决。
                    // 好像只有 String 类型的 不需要 设置成 Nullable 的类型;
                    int res = mySqlHelper.Update(car);
                }).Start();

                Task.Delay(100).ContinueWith((t) =>
                {
                    string sql = "SELECT * FROM car_info ;";
                    List<CarInfo> cars = mySqlHelper.Select<CarInfo>(sql);
                    foreach (var item in cars)
                    {
                        Debug.WriteLine("=======id:" + item.Id + " number:" + item.carNumber + " driver:" + item.driver + " phone:" + item.driverMobile + " status:" + item.deleteTime);
                    }
                });
            }
            

        }
        private void TestUpdate()
        {
          
        }

        private void InsertOrUpdate()
        {
           
        }

        private void TestTransation()
        {
       
        }

        private void TestExist()
        {
            
        }

        private void SqlBuild_Click(object sender, RoutedEventArgs e)
        {
           

        }
        /// <summary>
        /// test is  connection
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //  String connstr = ConfigurationManager.ConnectionStrings["mysqlConn"].ConnectionString.ToString();
            string ip = "127.0.0.1";
            int port= 3306;
            string username = "root";
            string pass = "Bnhq@1234";
            string db = "";
            string connstr = MysqlConnectionStringBuilder.GetConnectionString(ip, db, username, pass, port, true);
            MySqlHelper mySqlHelper = new MySqlHelper(connstr);
            if (mySqlHelper.IsConnecting())
            {
                Console.WriteLine("----IsConnected: True");
            }
            else
            {
                Console.WriteLine("----IsConnected: False");
            }

            if (mySqlHelper.IsOpened())
            {
                Console.WriteLine("----IsOpened: True");
            }
            else
            {
                Console.WriteLine("----IsOpened: False");
            }

          var res =  mySqlHelper.CreateDatabase("bnhq_debug");
        }

        private void Insert_Click(object sender, RoutedEventArgs e)
        {
           
        }



        private void TestSelect_Click(object sender, RoutedEventArgs e)
        {
            bool hasexception = false;

            while (hasexception == false)
            {
                try
                {
                    TestSelect();
                }
                catch (Exception ex)
                {

                    Console.WriteLine("+++++++++++++++++++++++" + ex.Message);
                    hasexception = true;
                    throw ex;
                }
               
            }
           
        }

 

  

        private void testTaskUpdate_Click(object sender, RoutedEventArgs e)
        {

            ThreadPool.QueueUserWorkItem( async (t) =>
            {
                String connstr = ConfigurationManager.ConnectionStrings["mysqlConn"].ConnectionString.ToString();
                MySqlHelper mySqlHelper = new MySqlHelper(connstr);
                DbHelper.enableErrorLog = true;
                DbHelper.isDebug = true;


                string sql = "SELECT * FROM car_info ;";
                List<CarInfo> cars = mySqlHelper.Select<CarInfo>(sql);


                CarInfo first = cars.First();
                first.driver = "测试修改100";
                int res = await AsyncSave(mySqlHelper, first);
                Console.WriteLine(res);

            });
        


        }

        private async Task<Int32> AsyncSave(MySqlHelper mySqlHelper,CarInfo car)
        {
            var  res =   mySqlHelper.Update(car);
            Console.WriteLine(res);
            return res;
        }

        private void test_dynamic_Click(object sender, RoutedEventArgs e)
        {
            //dynamic obj = new { };
            //obj.age = 23;
            //obj.name = "claf";
            //obj.salarey = 56.23;
            //obj["grilFrend"] = "luxi";


            //string json = JsonHelper.ObjectToJson(obj);

            //Console.WriteLine(json);

            Dictionary<String, object> data = new Dictionary<string, object>();

            data.Add("name", "clf");
            data.Add("age", 23);
            data.Add("weighing", 56.32);
            data.Add("bathday", DateTime.Now);

            string json = JsonHelper.ObjectToJson(data);

            Console.WriteLine(json);
        }

        private void SqliteBtn_Click(object sender, RoutedEventArgs e)
        {
            new SQLiteWindow().ShowDialog();
        }
    }
}
